﻿
// PVZCheaterDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "PVZCheater.h"
#include "PVZCheaterDlg.h"
#include "afxdialogex.h"
#include "tools.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


static int flag = true;
static DWORD SLEEP_TIME = 1000;
static HANDLE g_processHandle;
static CPVZCheaterDlg* g_dlg;
static Tools tools;
static HWND hwnd;


// CPVZCheaterDlg 对话框
CPVZCheaterDlg::CPVZCheaterDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PVZCHEATER_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPVZCheaterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SUN, m_sun);
	DDX_Control(pDX, IDC_KILL, m_kill);
	DDX_Control(pDX, IDC_CD, m_no_cd);
	DDX_Control(pDX, IDC_UNBREAKABLE, m_unbreakable);
	DDX_Control(pDX, IDC_BACKGROUND, m_background);
	DDX_Control(pDX, IDC_ZOOMBIE_UNBREAKABLE, m_zoombie_unbreakable);
	DDX_Control(pDX, IDC_COIN, m_coin);
	DDX_Control(pDX, IDC_CANCEL, m_cancel);
	DDX_Control(pDX, IDC_COLLECT_SUN, m_collect_sun);
	DDX_Control(pDX, IDC_SUN_RAIN, m_sun_rain);
	DDX_Control(pDX, IDC_DIAMOND, m_diamond);
}

BEGIN_MESSAGE_MAP(CPVZCheaterDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CANCEL, &CPVZCheaterDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_KILL, &CPVZCheaterDlg::OnBnClickedKill)
	ON_BN_CLICKED(IDC_UNBREAKABLE, &CPVZCheaterDlg::OnBnClickedUnbreakable)
	ON_BN_CLICKED(IDC_CD, &CPVZCheaterDlg::OnBnClickedCd)
	ON_BN_CLICKED(IDC_ZOOMBIE_UNBREAKABLE, &CPVZCheaterDlg::OnBnClickedZoombieUnbreakable)
	ON_BN_CLICKED(IDC_BACKGROUND, &CPVZCheaterDlg::OnBnClickedBackground)
	ON_BN_CLICKED(IDC_COLLECT_SUN, &CPVZCheaterDlg::OnBnClickedCollectSun)
	ON_BN_CLICKED(IDC_SUN_RAIN, &CPVZCheaterDlg::OnBnClickedSunRain)
	ON_BN_CLICKED(IDC_DIAMOND, &CPVZCheaterDlg::OnBnClickedDiamond)
	ON_BN_CLICKED(IDC_MONEY_PLUS_500, &CPVZCheaterDlg::OnBnClickedMoneyPlus500)
END_MESSAGE_MAP()




// CPVZCheaterDlg 消息处理程序

BOOL CPVZCheaterDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	g_dlg = this;
	AfxBeginThread(CPVZCheaterDlg::MonitoringThreadProc, this);
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}



// 用来监控植物大战僵尸的线程
UINT CPVZCheaterDlg::MonitoringThreadProc(LPVOID lpParam)
{
	while (flag) {
		// 找窗口
		hwnd = NULL;
		hwnd = ::FindWindow(TEXT("MainWindow"), TEXT("植物大战僵尸中文版"));
		if (hwnd == NULL) {
			if (g_processHandle != NULL) {
				::CloseHandle(g_processHandle);
				g_processHandle = NULL;
			}
			// 禁止点击
			g_dlg->OnBnClickedCancel();
			g_dlg->EnableWindow(false);
			
		}else {
			// 找进程ID
			DWORD pid = NULL;
			::GetWindowThreadProcessId(hwnd, &pid);

			// 找进程句柄
			g_processHandle = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);

			// 开启点击
			g_dlg->EnableWindow(true);
		}

		// 如果需要无限阳光
		if (g_dlg->m_sun.GetCheck()){
			DWORD value = 9990;
			//tools.WriteMemory(g_processHandle, &value, sizeof(value), 0x6A9EC0, 0x320, 0x8, 0x0, 0x8, 0x144, 0x2c, 0x5560, -1);
			tools.WriteMemory(g_processHandle, &value, sizeof(value), 0x6A9EC0, 0x768, 0x5560, -1);
		}
		Sleep(SLEEP_TIME);
	}
	return 0;
}

void CPVZCheaterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if (nID == SC_CLOSE) {
		flag = false;
	}
	CDialogEx::OnSysCommand(nID, lParam);
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CPVZCheaterDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CPVZCheaterDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


// 一键取消
void CPVZCheaterDlg::OnBnClickedCancel() {
	g_dlg->m_sun.SetCheck(false);
	g_dlg->m_kill.SetCheck(false);
	g_dlg->m_no_cd.SetCheck(false);
	g_dlg->m_unbreakable.SetCheck(false);
	g_dlg->m_background.SetCheck(false);
	g_dlg->m_zoombie_unbreakable.SetCheck(false);
	g_dlg->m_sun_rain.SetCheck(false);
	g_dlg->m_collect_sun.SetCheck(false);
	g_dlg->m_diamond.SetCheck(false);

	g_dlg->OnBnClickedKill();
	g_dlg->OnBnClickedCd();
	g_dlg->OnBnClickedUnbreakable();
	g_dlg->OnBnClickedBackground();
	g_dlg->OnBnClickedZoombieUnbreakable();
	g_dlg->OnBnClickedSunRain();
	g_dlg->OnBnClickedCollectSun();
	g_dlg->OnBnClickedDiamond();
}

// 一键控件可控化
void CPVZCheaterDlg::EnableWindowButton(BOOL b) {
	g_dlg->m_sun.EnableWindow(b);
	g_dlg->m_unbreakable.EnableWindow(b);
	g_dlg->m_no_cd.EnableWindow(b);
	g_dlg->m_background.EnableWindow(b);
	g_dlg->m_kill.EnableWindow(b);
	g_dlg->m_zoombie_unbreakable.EnableWindow(b);
	g_dlg->m_cancel.EnableWindow(b);
	g_dlg->m_coin.EnableWindow(b);
	g_dlg->m_sun_rain.EnableWindow(b);
	g_dlg->m_collect_sun.EnableWindow(b);
	g_dlg->m_diamond.EnableWindow(b);
}

// 秒杀僵尸
void CPVZCheaterDlg::OnBnClickedKill() {
	BYTE value_change[] = { 0x2B, 0xFF, 0x90, 0x90 };
	BYTE value_orign[] = { 0x2B, 0x7C, 0x24, 0x20 };
	DWORD address = 0x0053130F;;
	if (g_dlg->m_kill.GetCheck()) {
		// 去除僵尸不死
		g_dlg->m_zoombie_unbreakable.SetCheck(FALSE);
		g_dlg->OnBnClickedZoombieUnbreakable();
		tools.WriteMemory(g_processHandle, &value_change, sizeof(value_change), address);
	}
	else {
		tools.WriteMemory(g_processHandle, &value_orign, sizeof(value_orign), address);
	}
}


// 植物不死
void CPVZCheaterDlg::OnBnClickedUnbreakable() {
	BYTE value_change[] = { 0x90, 0x90, 0x90, 0x90 };
	BYTE value_orign[] = { 0x83, 0x46, 0x40, 0xFC };
	DWORD address = 0x0052FCF0;;
	if (g_dlg->m_unbreakable.GetCheck()) {
		tools.WriteMemory(g_processHandle, &value_change, sizeof(value_change), address);
	}
	else {
		tools.WriteMemory(g_processHandle, &value_orign, sizeof(value_orign), address);
	}
}

// 无CD
void CPVZCheaterDlg::OnBnClickedCd() {
	DWORD address = 0x487296;
	BYTE value_change[] = { 0x90, 0x90 };
	BYTE value_orign[] = { 0x7E, 0x14 };
	tools.ChangeMemory(g_processHandle, this->m_no_cd, address, value_orign, value_change, sizeof(value_change));
}

// 僵尸不死
void CPVZCheaterDlg::OnBnClickedZoombieUnbreakable() {
	DWORD address1 = 0x53130F;
	if (this->m_zoombie_unbreakable.GetCheck()) { // 如果需要僵尸不死
	  // 去除秒杀僵尸
		g_dlg->m_kill.SetCheck(FALSE);
		g_dlg->OnBnClickedKill();

		BYTE data1[] = { 0x83, 0xEF, 0x00, 0x90 };
		tools.WriteMemory(g_processHandle, data1, sizeof(data1), address1);
	}
	else { // 如果不需要僵尸不死
		BYTE data1[] = { 0x2B, 0x7C, 0x24, 0x20 };
		tools.WriteMemory(g_processHandle, data1, sizeof(data1), address1);
	}
}

// 后台运行
void CPVZCheaterDlg::OnBnClickedBackground() {
	DWORD address = 0x54E1C2;
	BYTE value_change[] = { 0x90, 0x90, 0x90 };
	BYTE value_orign[] = { 0x0F, 0x95, 0xC0 };
	tools.ChangeMemory(g_processHandle, this->m_background, address, value_orign, value_change, sizeof(value_change));
}

// 自动收集阳光
void CPVZCheaterDlg::OnBnClickedCollectSun() {
	DWORD address = 0x0043158F;
	BYTE value_change[] = { 0xEB };
	BYTE value_orign[] = { 0x75 };
	tools.ChangeMemory(g_processHandle, this->m_collect_sun, address, value_orign, value_change, sizeof(value_change));
}

// 阳光雨
void CPVZCheaterDlg::OnBnClickedSunRain() {
	DWORD address = 0x00413B83;
	BYTE value_change[] = { 0x90, 0x90 };
	BYTE value_orign[] = { 0x75, 0x6C };
	if (g_dlg->m_sun_rain.GetCheck()) {
		g_dlg->m_diamond.SetCheck(false);
		g_dlg->OnBnClickedDiamond();
		tools.WriteMemory(g_processHandle, &value_change, sizeof(value_change), address);
	}
	else {
		tools.WriteMemory(g_processHandle, &value_orign, sizeof(value_orign), address);
	}
}


// 砖石雨
void CPVZCheaterDlg::OnBnClickedDiamond() {
	DWORD address1 = 0x00413BD8;
	DWORD address2 = 0x00413B83;
	if (this->m_diamond.GetCheck()) {
		g_dlg->m_sun_rain.SetCheck(false);
		g_dlg->OnBnClickedSunRain();
		BYTE value_change1[] = { 0xB8, 0x03, 0x00, 0x00, 0x00 };
		BYTE value_change2[] = { 0x90, 0x90 };
		tools.WriteMemory(g_processHandle, value_change1, sizeof(value_change1), address1);
		tools.WriteMemory(g_processHandle, value_change2, sizeof(value_change2), address2);
	}
	else { 
		BYTE value_orign1[] = { 0xB8, 0x04, 0x00, 0x00, 0x00 };
		BYTE value_orign2[] = { 0x75, 0x6C };
		tools.WriteMemory(g_processHandle, value_orign1, sizeof(value_orign1), address1);
		tools.WriteMemory(g_processHandle, value_orign2, sizeof(value_orign2), address2);
	}
}


// 按一下+500金币
//mov eax, [006A9EC0]
//mov eax, [eax + 768]
//add[eax + 5560], 1F4
//ret
void CPVZCheaterDlg::OnBnClickedMoneyPlus500() {
	Tools::paramdata pdata = {0, 0, 0, 0};
	tools.AllocMemory(g_processHandle, true);
	BYTE b[] = { 0xA1, 0xC0, 0x9E, 0x6A,
		0x00, 0x8B, 0x80, 0x68, 0x07,
		0x00, 0x00, 0x81, 0x80, 0x60,
		0x55, 0x00, 0x00, 0xF4, 0x01, 0x00, 0x00, 0xC3 };
	tools.DoFunc(g_processHandle, b, pdata);
	tools.AllocMemory(g_processHandle, false);
}